﻿using System.Data;
using System.Collections.Generic;

namespace CounterServerModel
{
    /// <summary>
    /// A class that represents an account.
    /// </summary>
    public class AccountInfo
    {
        /// <summary>
        /// The ID of the account.
        /// </summary>
        public int AccountID { get; set; }

        /// <summary>
        /// The country of the account.
        /// </summary>
        public string Country { get; set; }

        /// <summary>
        /// The city of the account.
        /// </summary>
        public string City { get; set; }

        /// <summary>
        /// The street of the account.
        /// </summary>
        public string Street { get; set; }

        /// <summary>
        /// The house of the account.
        /// </summary>
        public int House { get; set; }

        /// <summary>
        /// The apartment of the account.
        /// </summary>
        public int? Apartment { get; set; }

        /// <summary>
        /// The phone number of the account.
        /// </summary>
        public string Phone { get; set; }

        /// <summary>
        /// The longitude of the account.
        /// </summary>
        public double Longitude { get; set; }

        /// <summary>
        /// The latitude of the account.
        /// </summary>
        public double Latitude { get; set; }

        /// <summary>
        /// Users associated with this account.
        /// </summary>
        public List<int> UsersAssociatedWith { get; set; }

        public AccountInfo(int i_AccountID)
        {
            AccountID = i_AccountID;

            UsersAssociatedWith = new List<int>();
        }

        public AccountInfo()
            : this(-1)
        {
        }

        /// <summary>
        /// Creates an AccountInfo based on the given DataRow.
        /// </summary>
        /// <param name="i_DataRow">The DataRow to create the account from.</param>
        /// <returns>An AccountInfo based on the given DataRow.</returns>
        public static AccountInfo CreateFromAccountRowAndUsersColumn(DataRow i_DataRow, DataTable i_AccountsUsersTable)
        {
            AccountInfo accountInfo = new AccountInfo(int.Parse(i_DataRow["AccountID"].ToString()));
            accountInfo.Country = i_DataRow["Country"].ToString();
            accountInfo.City = i_DataRow["City"].ToString();
            accountInfo.Street = i_DataRow["Street"].ToString();
            accountInfo.House = int.Parse(i_DataRow["House"].ToString());
            if (!string.IsNullOrEmpty(i_DataRow["Apartment"].ToString()))
            {
                accountInfo.Apartment = int.Parse(i_DataRow["Apartment"].ToString());
            }
            accountInfo.Phone = i_DataRow["Phone"].ToString();
            accountInfo.Longitude = double.Parse(i_DataRow["Longitude"].ToString());
            accountInfo.Latitude = double.Parse(i_DataRow["Latitude"].ToString());
            accountInfo.UsersAssociatedWith.AddRange(GetAssociatedUserInfos(i_AccountsUsersTable));

            return accountInfo;
        }

        private static List<int> GetAssociatedUserInfos(DataTable i_AccountsUsersTable)
        {
            List<int> userInfos = new List<int>();

            foreach (DataRow row in i_AccountsUsersTable.Rows)
            {
                userInfos.Add(int.Parse(row["UserID"].ToString()));
            }

            return userInfos;
        }
    }
}
